import 'package:xvideos/hj_model/video_model.dart';

import '../../../hj_model/acg/comic_chapters_model.dart';
import '../../../hj_model/cartoon_media_info.dart';
import '../../../hj_model/media_content.dart';

VideoModel changeData(CartoonMediaInfo? mediaInfo, ComicChapterInfo? curModel) {
  VideoModel newModel = VideoModel();
  if (mediaInfo == null) {
    return newModel;
  }
  int episode = 0;
  for (int i = 0; i < (mediaInfo.episodeList?.length ?? 0); i++) {
    if (mediaInfo.episodeList![i].id == curModel?.id) {
      episode = i;
      break;
    }
  }
  newModel.videoType = 1;
  newModel.id = mediaInfo.id;
  newModel.summary = mediaInfo.summary;
  newModel.totalEpisode = mediaInfo.totalEpisode;
  newModel.updateStatus = mediaInfo.updateStatus;
  newModel.mediaType = mediaInfo.mediaType;
  newModel.mediaInfo = mediaInfo;
  newModel.title = mediaInfo.title;
  newModel.tags = mediaInfo.tagDetails;
  newModel.cover = mediaInfo.coverH;
  newModel.likeCount = mediaInfo.countCollect; // 视频likecount是收藏数
  newModel.freeTime = mediaInfo.freeTime ?? 0;
  newModel.collectCount = mediaInfo.countCollect;
  newModel.commentCount = mediaInfo.countComment;
  newModel.playCount = mediaInfo.countView;
  newModel.playCount = mediaInfo.countBrowse;

  newModel.freeArea = mediaInfo.permission == 2;
  newModel.coins = mediaInfo.price;
  newModel.originCoins = mediaInfo.price;

  VidStatus vidStatus = VidStatus();
  vidStatus.hasPaid = mediaInfo.mediaStatus?.hasPaid == true ? true : false;
  vidStatus.hasCollected = mediaInfo.mediaStatus?.hasCollected;
  vidStatus.hasLiked = mediaInfo.mediaStatus?.hasLiked;
  vidStatus.hasDisliked = mediaInfo.mediaStatus?.hasDisliked;
  newModel.vidStatus = vidStatus;

  if (episode < (mediaInfo.episodeList?.length ?? 0)) {
    // newModel.originCoins = (episodeModel.listenPermission == 1) ? episodeModel.price : 0;
    // newModel.coins = (episodeModel.listenPermission == 1) ? episodeModel.price : 0;
    ComicChapterInfo episodeModel = mediaInfo.episodeList![episode];
    MediaContent? content = episodeModel.mediaContent;
    newModel.subid = content?.id;
    newModel.name = content?.name;
    newModel.sourceURL = content?.videoUrl;
    newModel.playTime = content?.playTime;
  }
  return newModel;
}
